package com.pug.config.loadbalance;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * Author: lqm
 * create: 2022/4/26 10:20
 * Lover: lqx
 *
 * @see
 */
//@LoadBalancerClients({
//        @LoadBalancerClient(value = "xq-pug-order-service", configuration = RandomLoadConfig.class),
//})     针对哪个服务使用哪个负载均衡算法
@Configuration
public class LoadbalancerConfig {

}
//随机
class RandomLoadConfig{
    @Bean
    public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory) {
        // 拿到你要处理的服务实列 其实也就是：xq-pug-user-service
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        // 你选择什么样子的策略来执行你的服务 RandomLoadBalancer 选择随机
        return new RandomLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
    }
}
//轮询
class PollLoadConfig{

}
